from selenium import webdriver
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', filename='../logs/login.log', filemode='w')
# 公共类，封装了页面操作的公共方法
class Base:
    # 初始化操作
    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.driver.get('http://192.168.88.100:8000/admin')
    # 封装定位元素方法(传递定位方式和对应的值)
    def base_find_element(self, loc):  # loc 元组
        logging.info(f'定位元素：{loc}')
        return self.driver.find_element(*loc) # *loc 元组解包
    # 封装输入文本(定位元素，输入的值)
    def base_input(self, loc, value):
        logging.info(f'输入文本：{value}')
        el = self.base_find_element(loc)
        el.clear()
        el.send_keys(value)
    # 封装点击操作
    def base_click(self, loc):
        logging.info(f'点击元素：{loc}')
        self.base_find_element(loc).click()
    # 封装获取文本
    def base_get_text(self, loc):
        logging.info(f'获取文本：{loc}')
        return self.base_find_element(loc).text
    # 关闭浏览器
    def base_close(self):
        self.driver.quit()

if __name__ == '__main__':
    b = Base()  # 实例化对象
    from selenium.webdriver.common.by import By
    from time import sleep
    loc = (By.ID, 'id_username')  # 用户名文本框
    b.base_input(loc, 'hello')
    sleep(3)
    b.base_close()
